package com.citi.xylophone.phoneEmulator.htmlExecutor;

import com.citi.xylophone.phoneEmulator.htmlExecutor.model.ExecutorResult;
import com.citi.xylophone.phoneEmulator.model.SourceAudio;
import com.citi.xylophone.phoneEmulator.model.xml.vxml.Audio;
import com.citi.xylophone.phoneEmulator.model.xml.vxml.VXML;
import com.thoughtworks.xstream.XStream;

public class FactoryInstructionResult {
	
	public static ExecutorResult generatedInstructionResult(String stringVXML){
		
		stringVXML = stringVXML.replace("<?xml version=\"1.0\" encoding=\"utf-8\"?>", "");
		XStream xstream = new XStream();
		xstream.processAnnotations(VXML.class);
		VXML vxml = (VXML)xstream.fromXML(stringVXML);
		
		if (vxml.getForm()!=null && vxml.getForm().getBlock()!=null && vxml.getForm().getBlock().getSubmit()!=null){
			return generatedResultInit(vxml);
		}
		
		if (vxml.getMenu()!=null){
			return generatedResultMenu(vxml);
		}
		
		if (vxml.getForm()!=null && vxml.getForm().getField()!=null){
			return generatedResultInput(vxml);
		}
		
		if (vxml.getForm()!=null && vxml.getForm().getBlock()!=null){
			return generatedResultNext(vxml);
		}
		
		if (vxml.getForm()!=null && vxml.getForm().getTransfer()!=null){
			return generatedResultTransfer(vxml);
		}
		
		
		
		return null;
	}
	
	
	

	private static ExecutorResult generatedResultInit(VXML vxml) {
		
		ExecutorResult executorResult = new ExecutorResult();
		executorResult.setType("init");
		executorResult.setVxml(vxml);
		return executorResult;
	}




	private static ExecutorResult generatedResultTransfer(VXML vxml) {
		
		ExecutorResult executorResult = new ExecutorResult();
		executorResult.setType("transfer");
		executorResult.setVxml(vxml);
		return executorResult;
	}




	private static ExecutorResult generatedResultNext(VXML vxml) {
		
		ExecutorResult executorResult = new ExecutorResult();
		executorResult.setType("next");
		executorResult.setVxml(vxml);
		
		for (Audio audio:vxml.getForm().getBlock().getPrompt().getAudioList()){
			executorResult.getAudios().add(new SourceAudio(audio.getSrc()));
		}
		
		return executorResult;
	}




	private static ExecutorResult generatedResultInput(VXML vxml) {
		
		ExecutorResult executorResult = new ExecutorResult();
		executorResult.setType("input");
		executorResult.setVxml(vxml);
		
		for (Audio audio:vxml.getForm().getField().getPrompt().getAudioList()){
			executorResult.getAudios().add(new SourceAudio(audio.getSrc()));
		}
		
		return executorResult;
	}

	private static ExecutorResult generatedResultMenu(VXML vxml) {
		
		ExecutorResult executorResult = new ExecutorResult();
		executorResult.setType("menu");
		executorResult.setVxml(vxml);
		
		//GET AUDIO OPTIONS		
		for (Audio audio:vxml.getMenu().getPrompt().getAudioList()){
			executorResult.getAudios().add(new SourceAudio(audio.getSrc()));
		}
		
		
		return executorResult;
	}

}
